//
// Created by 86131 on 2023/12/18.
//
#include "../DirectedAcyclicGraph.h"
#include <iostream>
using namespace std;
int main(){
    int num;
    cin>>num;
    DirectedAcyclicGraph graph(num);
    for(int i=0;i<num;i++){
        int m;
        cin>>m;
        if(m==0){ continue;}
        for(int j=0;j<m;j++){
            int end;
            cin>>end;
            graph.addEdge(i,end-1,1);
        }
    }
    vector<int> sortedOrder = graph.topologicalSort();
    cout<<sortedOrder[sortedOrder.size()-1];
}